package com.xnccc.audioset;

import java.io.File;

import com.owen.mapx.MapX;
import com.owen.mapxset.StarIIC;
import com.xnccc.fragment.BaseEqFragment;
import com.xnccc.fragment.BaseTestFragment;
import com.xnccc.fragment.DelayFragment;
import com.xnccc.fragment.DpFragment;
import com.xnccc.fragment.EqFragment;
import com.xnccc.fragment.NoiseFragment;
import com.xnccc.fragment.ResetFragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;

public class MainActivity extends FragmentActivity implements OnClickListener {
	public static MapX bird = null;

	private FragmentManager fm;
	private View uView = null;
	public static File yxFile;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		yxFile = new File(getCacheDir(), "yxpackage.json");
		bird = new MapX(new StarIIC());
		fm = getSupportFragmentManager();
		initView();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {

		if (keyCode == KeyEvent.KEYCODE_MENU) {
			startActivity(new Intent(MainActivity.this, ChangeDialog.class));
		}

		return super.onKeyDown(keyCode, event);
	}

	private void initView() {
		View vv = findViewById(R.id.btnVolumn);
		vv.setOnClickListener(this);
		vv.requestFocus();
		vv.performClick();
		findViewById(R.id.btnEQ).setOnClickListener(this);
		findViewById(R.id.btnBass).setOnClickListener(this);
		findViewById(R.id.btnDelay).setOnClickListener(this);
		findViewById(R.id.btnNoise).setOnClickListener(this);
		findViewById(R.id.btnReset).setOnClickListener(this);
		findViewById(R.id.btnBaseTest).setOnClickListener(this);
	}

	public void changePage(Fragment f) {
		fm.beginTransaction().replace(R.id.content_view, f).commit();
	}

	@Override
	public void onClick(View v) {
		if (uView != null) {
			uView.setSelected(false);
		}
		v.setSelected(true);
		uView = v;

		switch (v.getId()) {
		case R.id.btnVolumn:
			changePage(new DpFragment());
			break;
		case R.id.btnDelay:
			changePage(new DelayFragment());
			break;
		case R.id.btnNoise:
			changePage(new NoiseFragment());
			break;
		case R.id.btnEQ:
			changePage(new EqFragment());
			break;
		case R.id.btnBass:
			changePage(new BaseEqFragment());
			break;
		case R.id.btnBaseTest:
			changePage(new BaseTestFragment());
			break;
		case R.id.btnReset:
			changePage(new ResetFragment());
			break;
		}
	}

	@Override
	protected void onDestroy() {
		bird.switchSRC(MapX.SRC_ARM);
		sendBroadcast(new Intent("save_myself_yx"));
		super.onDestroy();
	}
}
